package cn.edu.jsu.zjj.service.impl;

import cn.edu.jsu.zjj.constant.CommonConstant;
import cn.edu.jsu.zjj.model.dto.operlog.OperLogQueryRequest;
import cn.edu.jsu.zjj.model.entity.OperLog;
import cn.edu.jsu.zjj.mapper.OperLogMapper;
import cn.edu.jsu.zjj.model.utils.SqlUtils;
import cn.edu.jsu.zjj.service.OperLogService;
import cn.edu.jsu.zjj.service.UserService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 操作日志记录 服务实现类
 * </p>
 *
 * @author fyt
 * @since 2024-05-12
 */
@Service
public class OperLogServiceImpl extends ServiceImpl<OperLogMapper, OperLog> implements OperLogService {

    @Autowired
    private UserService userService;


    @Override
    public QueryWrapper getQueryWrapper(OperLogQueryRequest operLogQueryRequest) {
        String oper_name = operLogQueryRequest.getOperName();
        //根据角色查询操作日志
        LocalDateTime startTime = operLogQueryRequest.getStartTime();
        LocalDateTime endTime = operLogQueryRequest.getEndTime();
        String remark = operLogQueryRequest.getRemark();
        String sortField = operLogQueryRequest.getSortField();
        String sortOrder = operLogQueryRequest.getSortOrder();
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(oper_name), "oper_name", oper_name);
        queryWrapper.between(startTime != null && endTime != null, "oper_time", startTime, endTime);
        queryWrapper.like(StrUtil.isNotBlank(remark), "remark", remark);
        queryWrapper.orderByDesc("oper_time");
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}
